/**
 * @see 办事指南界面
 */
import { timestampToTime } from '../../../utils/util';

Page({
  data: {
    oldShcoolHeadLine: [], // 保存一份公益讲坛数据,用于搜索
    shcoolHeadLine: [], // 查询办事指南列表
    appMsg: {
      type: '', // 状态
      show: false // 是否展示
    },
    questionnaireTitle: "",
    schoolCode: "",   //学校代码
    studentId: "",   //学号
    questionnaireId: "",   //问卷id
    titleValue: '' // 标题
  },
  onLoad () {
    this.setData({ userInfo: wx.getStorageSync('userInfo') })
    // this.getWorkList();
    this.qugetList();


  },
  openDetails (e) {
    let userId = wx.getStorageSync('userInfo').userId
    let schoolCode = wx.getStorageSync('schoolConfig').schoolCode
    let questionnaireId = e.currentTarget.dataset.item.questionnaireId;
    console.log(userId, "拿到学号");   //学号
    console.log(questionnaireId);  //问卷id


    // let { url, index } = e.currentTarget.dataset
    // this.data.shcoolHeadLine[index].clicks += 1;


    // this.setData({
    //   schoolCode: schoolCode,
    //   studentId: userId,
    //   questionnaireId: e.currentTarget.dataset.item.questionnaireId,
    // })

    wx.navigateTo({
      url: '/pages/questionnaireinvestigation/questionnaire/index?schoolCode=' + schoolCode + '&questionnaireId=' + questionnaireId + '&userId=' + userId
    })
    // wx.navigateTo({ url })
  },


  // 问卷调查列表
  qugetList () {
    let schoolCode = wx.getStorageSync('schoolConfig').schoolCode

    const request = {

      schoolCode,  // 学校code
      questionnaireTitle: this.data.questionnaireTitle

    }
    wx.$API['questionnaire/qugetList'](request).then((response) => {
      this.setData({
        appMsg: {
          type: 'loading',
          show: false
        }
      })
      const { data } = response
      console.log(response.data, "问卷调查列表");
      this.setData({
        shcoolHeadLine: response.data
      })
    }).catch((error) => {
      console.log(error)
      this.setData({
        appMsg: {
          type: 'err',
          show: true
        }
      })
    })
  },
  /**
   * @see 接口-获取办事指南列表
   */
  getWorkList () {
    let schoolCode = wx.getStorageSync('schoolConfig').schoolCode
    console.log(schoolCode, "拿到schoolcode代码");
    const request = {
      status: 1, // 1是已通过  2是未通过  3是待审核
      schoolCode,  // 学校code
      type: 2,

    }
    this.setData({
      appMsg: {
        type: 'loading',
        show: true
      }
    })
    wx.$API['workService/getShcoolHeadLineList'](request).then((response) => {
      this.setData({
        appMsg: {
          type: 'loading',
          show: false
        }
      })
      const { data } = response
      if (data.length === 0) {
        this.setData({
          appMsg: {
            type: 'none',
            show: true
          }
        })
      } else {
        data.forEach(item => {
          const { content, createTime } = item
          item.timeText = timestampToTime(createTime)
          item.date = item.timeText.split(' ')[0]
          item.time = item.timeText.split(' ')[1]
          let contentText = content.replace(/<[^<>]+>/g, '').replace(/&nbsp;/ig, '')
          if (contentText.length > 20) {
            item.contentText = contentText.substring(0, 20) + '...'
          } else {
            item.contentText = contentText
          }
        })
        this.setData({
          shcoolHeadLine: data,
          oldShcoolHeadLine: JSON.parse(JSON.stringify(data))
        })
      }
    }).catch((error) => {
      console.log(error)
      this.setData({
        appMsg: {
          type: 'err',
          show: true
        }
      })
    })
  },
  /**
   * @see 键盘回车时触发
   */
  search (e) {
    console.log(e.detail.value, "拿到");
    this.setData({
      questionnaireTitle: e.detail.value
    })
    this.qugetList();
    // const { oldShcoolHeadLine } = this.data
    // const { value } = e.detail
    // let newArr = []
    // oldShcoolHeadLine.forEach(headLineInfo => {
    //   if (headLineInfo.title.indexOf(value) > -1 || headLineInfo.content.indexOf(value) > -1) {
    //     newArr.push(
    //       JSON.parse(
    //         JSON.stringify(headLineInfo)
    //       )
    //     )
    //   }
    // })
    // if (newArr.length) {
    //   this.setData({
    //     shcoolHeadLine: newArr,
    //     titleValue: value
    //   })
    // } else {
    //   this.qugetList()
    // }
  },
  /**
   * @see 点击刷新界面
   */
  clickMsgButton () {
    this.qugetList()
  }
})